/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import network.packets.Packet;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class PacketBuilder 
{
    public static byte[] createLoginPacket(String login, String password, String clientVersion)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            byte[] loginData = StringUtils.strToBytes(login);
            byte[] passwordData = StringUtils.strToBytes(password);
            byte[] clientVersionData = StringUtils.strToBytes(clientVersion);
            
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + loginData.length + 4 + passwordData.length + 4 + clientVersionData.length));
             
            //ID пакета
            result.write(Utils.int16ToByte(Packet.LOGIN_REQUEST));
            
            //Логин и пароль
            result.write(Utils.intToByte(loginData.length));
            result.write(loginData);
            result.write(Utils.intToByte(passwordData.length));
            result.write(passwordData);
            
            //Версия клиента
            result.write(Utils.intToByte(clientVersionData.length));
            result.write(clientVersionData);
            
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }

    public static byte[] createDistrictsStatisticRequest()
    {
        return createPacketWithoutParams(Packet.DISTRICTS_STATISTIC_REQUEST);
    }

    //Метод генерирует запрос времени обновления списка районов на сервере
    public static byte[] createDistrictsUpdateServerTime()
    {
        return createPacketWithoutParams(Packet.DISTRICTS_UPDATE_SERVER_TIME);
    }

    public static byte[] createDistrictsListRequest()
    {
        return createPacketWithoutParams(Packet.DISTRICTS_LIST_REQUEST);
    }
    
    public static byte[] createOrdersListRequest()
    {
         return createPacketWithoutParams(Packet.ORDERS_LIST_REQUEST);
    }
    
    public static byte[] createPingResponce()
    {
        return createPacketWithoutParams(Packet.PING_RESPONCE);
    }

    public static byte[] createAdRequest()
    {
        return createPacketWithoutParams(Packet.AD_REQUEST);
    }

    public static byte[] createOrderRecievedResponce(int driverID, int orderID, int event)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4 + 2));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.ORDER_RECIEVED_RESPONCE));
            
            //ИД водителя
            result.write(Utils.intToByte(driverID));
            
            //ИД заказа
            result.write(Utils.intToByte(orderID));
            
            //Событие
            result.write(Utils.int16ToByte(event));
            
            return result.toByteArray();   
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createReacallRequest(int driverID, int orderID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.RECALL_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            //id заявки
            result.write(Utils.intToByte(orderID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createSendMessageRequest(int driverID, int destinationID, String messageText)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            byte[] textData = StringUtils.strToBytes(messageText);
            
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4 + 4 + textData.length));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.SEND_MESSAGE_REQUEST));
            
            //ИД водителя
            result.write(Utils.intToByte(driverID));
            
            //Кому сообщение
            result.write(Utils.intToByte(destinationID));
            
            //Текст сообщения
            result.write(Utils.intToByte(textData.length));
            result.write(textData);
            
            return result.toByteArray(); 
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createCancelOrderRequest(int driverID, int orderID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.CANCEL_ORDER_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            //id заявки
            result.write(Utils.intToByte(orderID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createCallToClientRequest(int driverID, int orderID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.CALL_TO_CLIENT_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            //id заявки
            result.write(Utils.intToByte(orderID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createOrderRefuseRequest(int driverID, int orderID) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.ORDER_REFUSE_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            //id заявки
            result.write(Utils.intToByte(orderID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createAlertRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.ALERT_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createMessagesArchiveRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.MESSAGES_ARCHIVE_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createCallToMeRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.CALL_TO_ME_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createExitRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));

            //ID пакета
            result.write(Utils.int16ToByte(Packet.EXIT_REQUEST));

            //ИД водителя :
            result.write(Utils.intToByte(driverID));

            return result.toByteArray();

        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }

    public static byte[] createMyInfoRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));

            //ID пакета
            result.write(Utils.int16ToByte(Packet.MY_INFO_REQUEST));

            //ИД водителя :
            result.write(Utils.intToByte(driverID));

            return result.toByteArray();

        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }

    public static byte[] createCloseRelayRequest(int driverID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.CLOSE_RELAY_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
            
            return result.toByteArray();
            
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createChangeDriverStateRequest(int driverID, int newState)
    {    
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 2));
            
            //ID пакета
            result.write(Utils.int16ToByte(Packet.CHANGE_DRIVER_STATE_REQUEST));
            
            //ИД водителя :
            result.write(Utils.intToByte(driverID));
                    
            //новое состояние водителя:
            result.write(Utils.int16ToByte(newState));
                    
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    public static byte[] createGetOrderRequest(int driverID,int orderID, int time)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4 + 2));
             
            //ID пакета
            result.write(Utils.int16ToByte(Packet.GET_ORDER_REQUEST));
            
            //ИД водителя 
            result.write(Utils.intToByte(driverID));
            
            //ИД заяки
            result.write(Utils.intToByte(orderID));
            
            //время 
            result.write(Utils.int16ToByte(time));
            
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        } 
    }
            
    
    public static byte[] createMoveToDistrictRequest(int driverID,int districtID)
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try
        {
            //Размер пакета
            result.write(Utils.intToByte(2 + 4 + 4));
             
            //ID пакета
            result.write(Utils.int16ToByte(Packet.MOVE_TODISTRICT_REQUEST));
            
            //ИД водителя
            result.write(Utils.intToByte(driverID));
                    
            //ИД района
            result.write(Utils.intToByte(districtID));
            
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                result.close();
            }catch(Exception ex){}
        }
    }
    
    private static byte[] createPacketWithoutParams(int id)
    {
        byte[] result = new byte[6];
        result[0] = 2;
        result[1] = 0;
        result[2] = 0;
        result[3] = 0;
        
        byte[] tmp = Utils.int16ToByte(id);
        result[4] = tmp[0];
        result[5] = tmp[1];
        
        return result;
    }
}
